package com.houhanzhi.controller;

import com.houhanzhi.test.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @PostMapping("/test1")
    public void initJcWxDeliverGoods(@RequestBody List<WeChatLogisticsInfoVO> logisticsInfoList) {
        log.info("初始化集采微信发货信息:{}", logisticsInfoList.size());
        List<WxDeliverGoodsDTO> resList = new ArrayList<>();

        // 获取当前时间，带时区偏移 +08:00
        OffsetDateTime dateTime = OffsetDateTime.now(ZoneOffset.ofHours(8));
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        String uploadTime = dateTime.format(formatter);
        // 先按 openId 分组，再按 outTradeNo 分组

        Map<String, Map<String, List<WeChatLogisticsInfoVO>>> groupedData = logisticsInfoList.stream()
                .filter(f -> StringUtils.isNotEmpty(f.getOpenId()))
                .filter(f -> StringUtils.isNotEmpty(f.getOutTradeNo()))
                .collect(Collectors.groupingBy(
                        WeChatLogisticsInfoVO::getOpenId, // 一级分组：按 openId
//                        Collectors.groupingBy(WeChatLogisticsInfoVO::getOrderId) // 二级分组：按 orderId
                        Collectors.groupingBy(WeChatLogisticsInfoVO::getOutTradeNo) // 二级分组：按 orderId
                ));

        // 输出结果
        groupedData.forEach((openId, orderMap) -> {
            orderMap.forEach((outTradeNo, items) -> {
                WxDeliverGoodsDTO result = new WxDeliverGoodsDTO();
                OrderKeyDTO orderKeyDTO = new OrderKeyDTO();
                orderKeyDTO.setOut_trade_no(outTradeNo);
                orderKeyDTO.setOrder_number_type(1);
                orderKeyDTO.setMchid("11111111");
                result.setOrder_key(orderKeyDTO);
                PayerDTO payerDTO = new PayerDTO();
                payerDTO.setOpenid(openId);
                result.setPayer(payerDTO);
                result.setDelivery_mode(2);
                result.setLogistics_type(1);
                result.setIs_all_delivered(true);
                result.setUpload_time(uploadTime);

                List<ShippingDTO> shippingList = new ArrayList<>();
                for (WeChatLogisticsInfoVO item : items) {
                    ShippingDTO shippingDTO = new ShippingDTO();
                    shippingDTO.setTracking_no(item.getTrackNo());
                    shippingDTO.setExpress_company(item.getTrackCode());
                    shippingDTO.setItem_desc(item.getSkuName() + "*" + item.getSkuNum());
                    shippingList.add(shippingDTO);
                }
                if (CollectionUtils.isEmpty(shippingList)) {
                    return;
                }
                result.setShipping_list(shippingList);
                resList.add(result);
            });
        });
        System.out.println("111222222222");
    }
}
